/*
	The MIT License

	Copyright (c) 2010 IFMO/GameDev Studio

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/

#include "d3d_local.h"

using namespace rapidxml;


IVertexBuffer *CreateStaticModel	( ED3DDriver *driver, const IPxTriMesh mesh );

/*-----------------------------------------------------------------------------
	Model manager :
-----------------------------------------------------------------------------*/

//
//	ED3DVBManager::ED3DVBManager
//
ED3DVBManager::ED3DVBManager( ED3DDriver *driver )
{
	LOG_INIT("Model manager");
	this->driver	=	driver;
	
	line_vb		=	NULL;
	line_decl	=	NULL;
	quad_decl	=	NULL;
}


//
//	ED3DVBManager::~ED3DVBManager
//
ED3DVBManager::~ED3DVBManager( void )
{
	LOG_SHUTDOWN("Model manager");
	
	SAFE_RELEASE(line_vb);
	SAFE_RELEASE(line_decl);
	SAFE_RELEASE(quad_decl);
}


//
//	ED3DVBManager::CreateEmptyModel
//
IPxVertexBuffer ED3DVBManager::CreateEmptyModel(void)
{
	return new ED3DVertexBuffer(this, driver);
}


/*-----------------------------------------------------------------------------
	Line rendering :
-----------------------------------------------------------------------------*/

struct line_vertex_s {
		EVec3	pos;
		EVec4	color;
		EVec2	uv;
	};

const D3DVERTEXELEMENT9 LINE_VERTEX_DECL[] = {
		{ 0, offsetof(line_vertex_s, pos	),	D3DDECLTYPE_FLOAT3,	D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION,	0 },
		{ 0, offsetof(line_vertex_s, color	),	D3DDECLTYPE_FLOAT4,	D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR,		0 },
		{ 0, offsetof(line_vertex_s, uv	),		D3DDECLTYPE_FLOAT2,	D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD,	0 },
		D3DDECL_END()
	};


//
//	ED3DVBManager::DrawLines
//
void ED3DVBManager::DrawLines( uint num_lines, const ERSLine_s *lines )
{
	IDirect3DDevice9	*d3ddev	=	driver->GetDevice();
	
	const uint	line_block_size	=	128;
	const uint	stride			=	sizeof(line_vertex_s);
	const uint	length			=	2*line_block_size * stride;
	const uint	usage			=	D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY;
	const D3DPOOL pool			=	D3DPOOL_DEFAULT;
	
	if (!line_vb) {
		HRCALL( d3ddev->CreateVertexBuffer( length, usage, 0, pool, &line_vb, NULL) )
	}
	
	if (!line_decl) {
		HRCALL( d3ddev->CreateVertexDeclaration( LINE_VERTEX_DECL, &line_decl ) );
	}
	
	HRCALL( d3ddev->SetVertexDeclaration( line_decl ) );

	//
	//	draw line blocks :
	//
	uint line_padding		=	(line_block_size - (num_lines % line_block_size)) % line_block_size;
	uint num_blocks			=	(num_lines + line_padding) / line_block_size;
	
	for (uint i=0; i<num_blocks; i++) {
	
		uint num_lines_to_draw	=	line_block_size;
		
		if (i==(num_blocks-1) && num_lines != line_block_size * num_blocks) {
			num_lines_to_draw	=	num_lines % line_block_size;
		}
	
		line_vertex_s *data;		
		HRCALL( line_vb->Lock(0, 0, (void**)&data, D3DLOCK_DISCARD) );
		
		memcpy( data, &lines[i * line_block_size], 2 * num_lines_to_draw * stride );
		
		line_vb->Unlock();

		HRCALL( d3ddev->SetStreamSource( 0, line_vb, 0, stride ) );
		HRCALL( d3ddev->DrawPrimitive( D3DPT_LINELIST, 0, num_lines_to_draw ) );
	}
}


//
//	ED3DVBManager::DrawQuads
//
void ED3DVBManager::DrawQuads( uint num_quads, const EUIQuad *quads )
{
	for (uint i=0; i<num_quads; i++) {
		const EUIQuad *q = &quads[i];
		
		EVec4	color = q->color;
		
		DrawQuad( q->p0.x,   q->p0.y,    q->p1.x,  q->p1.y,  0.5f,
				  q->uv0.x,	 q->uv0.y,   q->uv1.x, q->uv1.y, Vec4ToByte4(color) );
	}
}


//
//	ED3DVBManager::DrawQuad
//
void ED3DVBManager::DrawQuad( float x0, float y0, float x1, float y1, float z, float u0, float v0, float u1, float v1, uint32_t color4b )
{
	IDirect3DDevice9	*d3ddev	=	driver->GetDevice();

	struct vertex2d_hw_s {
			float	 x, y, z;
			uint32_t color;
			float	 u, v;
		};

	vertex2d_hw_s quad[4];

	quad[0].x = x0;	quad[0].y = y0;	quad[0].z = z;
	quad[0].u = u0; quad[0].v = v0; quad[0].color = color4b;

	quad[1].x = x1;	quad[1].y = y0;	quad[1].z = z;
	quad[1].u = u1; quad[1].v = v0; quad[1].color = color4b;

	quad[2].x = x1;	quad[2].y = y1;	quad[2].z = z;
	quad[2].u = u1; quad[2].v = v1; quad[2].color = color4b;

	quad[3].x = x0;	quad[3].y = y1;	quad[3].z = z;
	quad[3].u = u0; quad[3].v = v1; quad[3].color = color4b;
	
	HRCALL( d3ddev->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1) );
	
	HRCALL( d3ddev->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, &quad, sizeof(vertex2d_hw_s)) );
}
